package com.dhcc.core.framework.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.security.ShiroKit;

/**
 * AOP 权限自定义检查
 * 
 * @ClassName: PermissionAop
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午9:44:09
 */
@Aspect
@Component
public class PermissionAop {

    @Pointcut(value = "@annotation(com.dhcc.core.framework.annotion.Permission)")
    private void cutPermission() {

    }

    @Around("cutPermission()")
    public Object doPermission(ProceedingJoinPoint point) throws Throwable {
        MethodSignature ms = (MethodSignature) point.getSignature();
        Method method = ms.getMethod();
        Permission permission = method.getAnnotation(Permission.class);
        Object[] permissions = permission.value();
        Object[] roles = permission.roles();
        boolean result = false;
        if (roles != null && roles.length > 0) {
            // 检查指定角色
            result = ShiroKit.checkRoles(roles);
        }
        if (permissions != null && permissions.length > 0) {
            // 检查全体角色
            result = ShiroKit.checkPermissions(permissions);
        }
        if (result) {
            return point.proceed();
        } else {
            throw new BizException(BizExceptionEnum.NO_PERMITION);
        }

    }

}
